<div class="open-form">
    <div class="open-model">
        <div class="open-auto">
            <!-- 头部操作栏 -->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">名称：</label>
                        <div class="layui-input-inline mr0">
                            <input name="name" class="layui-input" type="text" placeholder="请输入名称"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="productUnitTabFormSubSearchLog" lay-submit
                                layui-form-keyDownSearch="true"><i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="productUnitBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加
                        </button>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="productUnitTable" lay-filter="productUnitTable"></table>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </div>
</div>


<script type="text/html" id="productUnitTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="productUnitForm">
    <form lay-filter="productUnitForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">物料类别</label>
            <div class="layui-input-block">
                <select name="productTypeId" lay-filter="productType" lay-verify="required" lay-search disabled>
                    <option value=""> 请选择物料类别</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">计量方式</label>
            <div class="layui-input-block">
                <select id="calWay" name="calWay" lay-filter="calWay" lay-verify="required" lay-search></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入名称" type="text" class="layui-input" lay-verify="required"
                       required/>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">是否基础单位</label>
            <div class="layui-input-block">
                <input name="isBaseUnit" type="checkbox" lay-skin="switch" value="1" lay-text="是|否"
                       lay-filter="isBaseUnit"/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input name="isDeleted" type="checkbox" lay-skin="switch" value="1" checked="false"
                       lay-text="启用|停用"/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input name="sortNumber" placeholder="请输入排序" type="text" class="layui-input" lay-verify=""
                       required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入描述" class="layui-textarea"/>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="productUnitFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'laydate', 'admin', 'laytpl', 'tableTreeDj', 'config'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laydate = layui.laydate;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laytpl = layui.laytpl;
        var tableTree = layui.tableTreeDj;

        // 回显计量单位数据
        var productTypeData = admin.getTempData('productTypeData');

        // 渲染表格
        var insTb = table.render({
            elem: '#productUnitTable',
            url: config.base + 'enterprise/product/admin/productUnit/list?productType=' + productTypeData.id,
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers',title: '序号'}
                , {field: 'name', align: 'center', title: '单位名称'}
                , {field: 'code', align: 'center', title: '单位编码'}
                , {field: 'productTypeName', align: 'center', title: '物料类别'}
                , {field: 'calWayName', align: 'center', title: '计量方式'}
                , {
                    field: 'isBaseUnit', align: 'center', title: '是否基础单位', templet: function (d) {
                        if (d.isBaseUnit) {
                            return '是'
                        } else {
                            return '否'
                        }
                    }
                }
                , {field: 'remark', align: 'center', title: '描述'}
                , {field: 'createTime', align: 'center', title: '创建时间'}
                , {
                    field: 'isDeleted', align: 'center', title: '是否启用', templet: function (d) {
                        if (d.isDeleted) {
                            return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="' + d.id + '" checked="' + d.isDeleted + '" lay-filter="isDeleted"  lay-text="启用|停用"/>';
                        } else {
                            return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="' + d.id + '" lay-filter="isDeleted"   lay-text="启用|停用"/>';
                        }
                    }
                }
            ]],
            done: function (res, curr, count) {
            }
        });

        // 工具条点击事件
        table.on('tool(productUnitTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(productUnitTable)', function (obj) {
            table.reload('productUnitTable', {
                initSort: obj,
                where: {
                    sort: obj.field,
                    order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(productUnitTabFormSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        form.on('switch(isDeleted)', function (obj) {
            if (obj.elem.checked) {
                enable(obj.value)
            } else {
                disable(obj.value)
            }
        })

        function enable(id) {
            admin.req('enterprise/product/admin/productUnit/enable', {id: id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('productUnitTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        function disable(id) {
            admin.req('enterprise/product/admin/productUnit/disable', {id: id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('productUnitTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        function isShowAdd() {
            admin.req('enterprise/product/admin/productUnit/countByProductType', {productType: productTypeData.id}, function (res) {
                if (res.code == 200) {
                    if (parseInt(res.data) > 0) {
                        $("#productUnitBtnAdd").hide();
                    } else {
                        $("#productUnitBtnAdd").show();
                    }
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'GET');
        }

        // 添加按钮点击事件
        $('#productUnitBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            var windowIndex = admin.open({
                type: 1,
                area: '480px',
                offset: '150px',
                title: data ? '修改计量单位' : '添加计量单位',
                content: $('#productUnitForm').html(),
                success: function () {
                    //计量方式
                    admin.getDictList("calWay", "product_unit_cal_way", data ? data.calWay : null, "计量方式");

                    if (data == undefined || (data && data.isBaseUnit)) {
                        $("from input[name='baseRate']").removeAttr('lay-verify', 'required')
                        $("#baseRateDiv").hide();
                    } else if (data && !data.isBaseUnit) {
                        $("from input[name='baseRate']").attr('lay-verify', 'required')
                        $("#baseRateDiv").show();
                    }
                    form.render();

                    form.on('switch(isBaseUnit)', function (data) {
                        if (data.elem.checked) {
                            $("from input[name='baseRate']").removeAttr('lay-verify', 'required')
                            $("#baseRateDiv").hide();
                        } else {
                            $("from input[name='baseRate']").attr('lay-verify', 'required')
                            $("#baseRateDiv").show();
                        }
                    })

                    var option = '<option selected>' + productTypeData.name + '</option>'
                    $("form select[name='productTypeId']").html(option);
                    form.render('select')

                    form.val('productUnitForm', data);
                    // 表单提交事件
                    form.on('submit(productUnitFormSubmit)', function (d) {
                        layer.load(2);

                        var isDeleted = $("form input[name='isDeleted']").prop("checked");
                        if (!isDeleted) {
                            d.field.isDeleted = false;
                        } else {
                            d.field.isDeleted = true;
                        }

                        var isBaseUnit = $("form input[name='isBaseUnit']").prop("checked");
                        if (!isBaseUnit) {
                            d.field.isBaseUnit = false;
                        } else {
                            d.field.isBaseUnit = true;
                        }
                        d.field.productType = productTypeData.id;
                        admin.req(data ? 'enterprise/product/admin/productUnit/update' : 'enterprise/product/admin/productUnit/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('productUnitTable');
                                layer.close(windowIndex);
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/product/admin/productUnit/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

    });
</script>